package action.resource;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import service.interf.security.IResource;
import action.common.MyBaseAction;

import com.opensymphony.xwork2.ModelDriven;

import dao.pojo.Resource;

public class ResourceAction extends MyBaseAction<Resource> implements
		ModelDriven<Resource> {
	private static final long serialVersionUID = 1L;
	private IResource iResource;
	private String userName;

	/************************* setter and getter *******************************/
	public void setiResource(IResource iResource) {
		this.iResource = iResource;
	}

	public IResource getiResource() {
		return iResource;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public Resource getModel() {
		return model;
	}

	// constructor
	public ResourceAction() {
		super(Resource.class);
		validRules.clear();
		validRules.put("resourceType.resourceTypeId", "empty");
		validRules.put("url", "empty|url");
		validRules.put("title", "empty");
		validRules.put("datas", "empty");
	}

	/**************************** method ***************************************/

	/**
	 * 添加权限
	 */
	public void add() {
		String r = "1";
		try {
			iResource.addRecord(model);
		} catch (Exception e) {
			r = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), r);
	}

	/**
	 * 删除权限
	 */
	public void del() {
		if (!validMe("ids"))
			return;
		String r = "1";
		try {
			iResource.delByIds("Resource", "resourceId", model.getIds());
		} catch (Exception e) {
			r = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), r);
	}

	/**
	 * 批量保存
	 */
	public void batch() {
		if (!validMe("datas"))
			return;
		String r = "1";
		try {
			iResource.batchSave("Resource", model.getDatas(), null);
		} catch (Exception e) {
			r = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), r);
	}

	/**
	 * 获取资源 按照title即资源名称查询
	 */
	public void findAll() {
		String r = "";
		List<String> pNames = new ArrayList<String>();
		List<Object> pValues = new ArrayList<Object>();
		List<String> relations = new ArrayList<String>();
		if (null != model.getTitle()) {
			pNames.add("title");
			pValues.add(model.getTitle());
			relations.add("like");
		}
		if (null != model.getUrl()) {
			pNames.add("url");
			pValues.add(model.getUrl());
			relations.add("like");
		}
		String[] pName = (String[]) pNames.toArray(new String[0]);
		Object[] pValue = (Object[]) pValues.toArray(new Object[0]);
		String[] relation = (String[]) relations.toArray(new String[0]);
		try {
			r = iResource
					.findByProperty(
							"Resource",
							"*,resourceType.resourceTypeId(resourceTypeId),resourceType.resourceTypeName(resourceTypeName)",
							"json", true, pName, pValue, null, relation, model
									.getPage(), model.getRows());
		} catch (Exception e) {
			r = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient(ServletActionContext.getResponse(), r);
	}

	/**
	 * 修改资源
	 */
	public void editResource() {
		String x = "";
		try {
			model = (Resource) iResource.editRecord(model);
			x = model.getEntityId().toString();
		} catch (Exception e) {
			x = errorHandle.handleErr(e);
		}
		this.servletHandle
				.writeToClient1(ServletActionContext.getResponse(), x);
	}

	/**
	 * 依据resourceId获取单个商家信息
	 */
	public void getResourceById() {
		String r = null;
		// 保存ResourceId 到session
		ServletActionContext.getRequest().getSession().setAttribute(
				"resourceId", model.getResourceId());
		try {
			r = iResource
					.findByProperty(
							"Resource",
							"*,resourceType.resourceTypeId(resourceTypeId),resourceType.resourceTypeName(resourceTypeName)",
							"json", false, new String[] { "resourceId" },
							new Object[] { model.getResourceId() }, null, null,
							1, 1);
		} catch (Exception e) {
			r = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient(ServletActionContext.getResponse(), r);
	}
}
